<?php
namespace  Home\Controller;
use Think\Controller;
use Com\DingdingAuth;

class DomainController  extends  HomeController{
	
	public function index(){
		$user_info=session('DING_USER');
		if (!$user_info) {
			header('Location: '.U('Home/Task/index'));
		}
		// 搜索
		$keyword = I('keyword', '', 'string');
		$condition = array('like','%' . $keyword . '%');
		$map['name|id'] = array($condition,$condition,'_multi' => true );
		// 获取所有用户
		$data_list = D('Domain')->page(! empty($_GET["p"]) ? $_GET["p"] : 1, C('ADMIN_PAGE_ROWS'))->where($map)->order('id desc')->select();
		$page = new \Common\Util\Page(D('Domain')->where($map)->count(), C('ADMIN_PAGE_ROWS'));
		
		foreach ($data_list as $key => &$val) {
			$customerid=$val['customer'];
			$val['kh_id']=$customerid;
			$val['c_user']=get_user_info($val['c_user'],"username");
			$val['customer']=get_customer_info($val['customer'],"customer_name");
			 
			//$val['customer']="<a href='".U('customer/xq',array('id'=>$customerid))."' style='text-decoration:none'>".$val['customer']."</a>";
			$val['e_time']?$val['e_time']=date('Y-m-d',$val['e_time']):'';
			$tt1['tablename']='server_buyer';
			$tt1['fieldvalue']=$val['register'];
			$server_buyer=D('Descriptions')->where($tt1)->find();
			$val['register']=$server_buyer['title'];
		}


        $this->assign('HTTP_HOST', C('DING_HTTP_HOST'));
        $this->assign('page', $page->show());
        $this->assign('data_list', $data_list);
        $this->assign('user_info', $user_info);
        $this->assign('meta_title', '域名列表');
        $this->display();
	}
	
	
	public function add(){
		
		$user_info=session('DING_USER');
		if (!$user_info) {
			header('Location: '.U('Home/Task/index'));
		}
		if(IS_POST){
			
			$ding_id=$user_info['userid'];
			$uid=M('Employee')->where(array('ding_id'=>$ding_id))->getField('uid');
			$_POST['r_time']=strtotime($_POST['r_time']);
			$_POST['e_time']=strtotime($_POST['e_time']);
			$_POST['c_user']=$uid;
			
			$web=D('Domain');
			$data=$web->create();
			if($data){
				$id=$web->add();
				if($id){
					$this->success('新增成功',U('index'));
				}else{
					$this->error('新增失败');
				}
			}else{
				$this->error($web->getError());
			}

		}else{

			$supp=D('Descriptions')->where('tablename="server_buyer"')->field('fieldvalue as id,title')->select();
			$customer=M("Customer")->field("id,customer_name as title")->select();
				
				$this->assign('customer', $customer);
				$this->assign('supp', $supp);
				
				$this->assign('user_info', $user_info);
				$this->assign('meta_title', '新增域名');
				$this->display();

			}
	}
	
	public function  edit($id){
		$user_info=session('DING_USER');
		if (!$user_info) {
			header('Location: '.U('Home/Task/index'));
		}
		
			if(IS_POST){
			
				$server=M('Domain');
				
				$_POST['r_time']=strtotime($_POST['r_time']);
				
				$_POST['e_time']=strtotime($_POST['e_time']);
				
				
				
				$data=$server->create();
				
				if($data){
				
					$res=$server->save();
				
					if($res){
				
						$this->success('更新成功',U('index'));
				
					}else{
				
						$this->error('更新失败');
				
					}
				
				}else{
				
					$this->error($server->getError());
				
				}

			}else{
				$info=M('Domain')->find($id);
				$supp=D('Descriptions')->where('tablename="server_buyer"')->field('fieldvalue as id,title')->select();
				$customer=M("Customer")->field("id,customer_name as title")->select();
			
				$this->assign('info', $info);
				$this->assign('customer', $customer);
				$this->assign('supp', $supp);
				$this->assign('user_info', $user_info);
				$this->assign('meta_title', '编辑域名');
				$this->display();
			
			}

	}
	
	/**
	 * 详情页
	 */
	public function domaininfo(){
		
		$id=I('id');
		$val = M('Domain')->where('id='.$id)->find();
		$customerid=$val['customer'];
		
		$val['c_user']=get_user_info($val['c_user'],"username");
		$val['customer']=get_customer_info($val['customer'],"customer_name");
		
		$tt1['tablename']='server_buyer';
		$tt1['fieldvalue']=$val['register'];
		$server_buyer=D('Descriptions')->where($tt1)->find();
		$val['register']=$server_buyer['title'];
		
				
		$val['r_time']?$val['r_time']=date('Y-m-d',$val['r_time']):'';
		$val['e_time']?$val['e_time']=date('Y-m-d',$val['e_time']):'';
		
		
		$this->assign('val', $val);
		$this->assign('meta_title', '域名详情');
		$this->display();
		
	}
	/**
	 * 删除域名
	 */
// 	public function delDomainAjax(){
	
// 		$id = I('id');
// 		$re=M('Domain')->where('id='.$id)->delete();
// 		if ($re) {
// 			$data['info']=1;
// 		}else{
// 			$data['info']=0;
// 		}
// 		$this->ajaxReturn($data);
// 	}
	


}


?>